/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.twistinjava.ajax;

import com.twistinjava.category.model.Category;
import com.twistinjava.category.modelDAO.CategoryDAO;
import com.twistinjava.exception.TwistinjavaException;
import com.twistinjava.logon.User;
import com.twistinjava.userDAO.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.Document;
import org.apache.commons.logging.*;
/**
 *
 * @author shikhar
 */
public class Edit extends HttpServlet {
    private static Log log = LogFactory.getLog(Edit.class);
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        log.debug("inside Edit servlet****");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        StringBuilder htmlString = new StringBuilder();
          UserDAO userDAO = new UserDAO();
          CategoryDAO categoryDAO = new CategoryDAO();
         String responseStr = null;
        try {
             String method = request.getParameter("method");

                    if (method.equals("editUser"))
                    {
                        log.debug("inside  editUser method of Edit servlet");
                        String id = request.getParameter("id");
                        String counter= request.getParameter("counter");
                      
                        User user = userDAO.getUser(Integer.parseInt(id));
                        htmlString.append("<table border='0' cellpadding='0' cellspacing='0' width='500px' >")
                                .append("<tr onMouseDown='mousePressed()' onMouseUp='mouseReleased()'>")
                                .append("<td width='15'>&nbsp;</td>")
                                .append("<td class='popUpHeader' style='cursor: move'>Edit User</td>")
                                .append("<td width='15' valign='top'><img src='/images/cross1.jpg' width='29' height='22' onClick='javascript:closeDiv()' style='cursor: pointer'>")
                                .append("</td>").append("</tr>").append("<tr>").append("<td width='15'>&nbsp;</td>")
                                .append("<td class='popUpHeader'>")
                                .append("<table border='0' cellpadding='0' cellspacing='0' width='100%' class='editUser' >")
                                .append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>First Name<font color='red'>*</font></td>")
                                .append("<td><input name='fName' id='fName' value='").append(user.getFirstName()).append("' size='30'></td><td>&nbsp;</td>")
                                .append("</tr>")
                                .append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>Last Name<font color='red'>*</font></td>")
                                .append("<td><input name='lName' id='lName' value='").append(user.getLastName()).append("' size='30'></td><td>&nbsp;</td>")
                                .append("</tr>")
                                
                                .append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>Email<font color='red'>*</font></td>")
                                .append("<td><input name='email' id='email' value='").append(user.getEmail()).append("' size='30'></td><td>&nbsp;</td>")
                                .append("</tr>")
                                
                                .append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>Login ID<font color='red'>*</font></td>")
                                .append("<td><input name='loginID' id='loginID' value='").append(user.getLogonID()).append("' size='30' onchange='checkLoginID()'></td><td id='wait'></td>")
                                .append("</tr>")
                                
                                .append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>Password<font color='red'>*</font></td>")
                                .append("<td><input type='password' name='password' id='password' value='").append(user.getPassword()).append("' size='30'></td><td>&nbsp;</td>")
                                .append("</tr>");

    
                        htmlString.append("</td>").append("</tr>").append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>&nbsp;</td><td colSpan='2'><input type='submit' name='update' value='Update' onclick=updateUser('").append(user.getId()).append("'").append(")><input type='hidden' name='counter' id='counter' value='").append(counter).append("'><input type='hidden' id='id' name='id' value=')").append(id).append("'></td>").append("</tr>").append("</table>").append("</td>").append("<td width='15' valign='top' colspan='3'>&nbsp;</td>").append("</tr>").append("</table>");
                        responseStr = htmlString.toString();
                    }
                    else if(method.equals("save"))
                    {
                        log.debug("Inside Save method");
                        User user = new User();
                        user.setLogonID(request.getParameter("loginID"));
                        user.setFirstName(request.getParameter("fName"));
                        user.setLastName(request.getParameter("lName"));
                        user.setEmail(request.getParameter("email"));
                        user.setPassword(request.getParameter("password"));
                        user.setId(Integer.parseInt(request.getParameter("id")));

                        userDAO.updateUser(user);
                        responseStr="save";
                      
                    }
                    else if(method.equals("editCategory"))
                    {
                        log.debug("inside  editCategory method of Edit servlet");
                        String id = request.getParameter("id");
                        String counter= request.getParameter("counter");

                        Category category = categoryDAO.getCategory(Integer.parseInt(id));
                        htmlString.append("<table border='0' cellpadding='0' cellspacing='0' width='500px' >")
                                .append("<tr onMouseDown='mousePressed()' onMouseUp='mouseReleased()'>")
                                .append("<td width='15'>&nbsp;</td>")
                                .append("<td class='popUpHeader' style='cursor: move'>Edit User</td>")
                                .append("<td width='15' valign='top'><img src='/images/cross1.jpg' width='29' height='22' onClick='javascript:closeDiv()' style='cursor: pointer'>")
                                .append("</td>").append("</tr>").append("<tr>").append("<td width='15'>&nbsp;</td>")
                                .append("<td class='popUpHeader'>")
                                .append("<table border='0' cellpadding='0' cellspacing='0' width='100%' class='editUser' >")
                                .append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>Category Name<font color='red'>*</font></td>")
                                .append("<td><input name='cName' id='cName' value='").append(category.getCategoryName()).append("' size='30' onchange='checkCategory()'></td><td id='wait' width='70'></td>")
                                .append("</tr>");

                        htmlString.append("</td>").append("</tr>").append("<tr>").append("<td width='20'>&nbsp;</td>").append("<td>&nbsp;</td><td colSpan='2'><input type='submit' name='update' value='Update' onclick=updateCategory('").append(category.getId()).append("'").append(")><input type='hidden' name='counter' id='counter' value='").append(counter).append("'><input type='hidden' id='id' name='id' value=')").append(id).append("'></td>").append("</tr>").append("</table>").append("</td>").append("<td width='15' valign='top' colspan='3'>&nbsp;</td>").append("</tr>").append("</table>");
                        responseStr = htmlString.toString();
                    }
                  else if(method.equals("saveCategory"))
                    {
                        log.debug("Inside saveCategory method");
                        Category category = new Category();
                        category.setCategoryName(request.getParameter("cName"));
                        category.setId(Integer.parseInt(request.getParameter("id")));

                        categoryDAO.updateCategory(category);
                        responseStr="save";

                    }
            out.write(responseStr);
        }
        catch(TwistinjavaException ex)
        {
            log.error("Exception inside Edit servlet **********"+ ex.getMessage());
            responseStr="error";
            out.write(responseStr);
        }

        finally {
            out.close();
        }
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
